from cgitb import enable
from dataclasses import dataclass
from runpy import run_module
import MySQLdb


def mysrl_link():  # 连接数据库
    #数据库地址
    db = MySQLdb.connect("ip", "user", "passwd","smart_lock_base", charset='utf8')
    return db




def mysql_add_Student(sno, sname, sroot='1', spasswd='2241'):
    db = mysrl_link()
    cursor = db.cursor()
    # 使用execute方法执行SQL语句
    sql = "INSERT INTO Student (`Sno`, `Sname`, `Sroot`, `Spasswd`) VALUES ('%s', '%s', '%s', '%s');" % (
        sno, sname, sroot, spasswd)
    try:
        # 执行sql语句
        cursor.execute(sql)
    # 提交到数据库执行
        db.commit()
        flag = 0
    except:
        print("数据库操作失败")
        # Rollback in case there is any error
        db.rollback()
        flag = -1
    db.close()
    return flag


if __name__ == '__main__':
    pass